package com.lottery.im.module.chat.ui;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.lottery.im.R;
import com.lottery.im.api.result.entity.MemberVipInfo;
import com.lottery.im.base.BaseSwipeFragment;
import com.lottery.im.module.chat.presenter.MemberProfilePresenter;
import com.lottery.im.module.chat.view.MemberProfileView;
import com.lottery.im.module.tim.model.FriendshipInfo;
import com.lottery.im.module.tim.presenter.FriendshipManagerPresenter;
import com.lottery.im.module.tim.viewfeatures.FriendshipManageView;
import com.lottery.im.util.StatusBarHelper;
import com.lottery.im.util.transformations.GlideCircleBorderTransform;
import com.lottery.im.widget.ShSwitchView;
import com.lottery.im.widget.dialog.EditDialogBuilder;
import com.tencent.imsdk.TIMGroupMemberRoleType;
import com.tencent.imsdk.ext.sns.TIMFriendStatus;

/**
 * 群内成员资料（可操作禁言、设置管理员等）
 */
public class MemberProfileFragment extends BaseSwipeFragment<MemberProfilePresenter> implements
    MemberProfileView, FriendshipManageView {

  private final static String INFO_KEY = "INFO_KEY";

  public static MemberProfileFragment create(MemberVipInfo info) {
    MemberProfileFragment fragment = new MemberProfileFragment();
    Bundle b = new Bundle();
    b.putSerializable(INFO_KEY, info);
    fragment.setArguments(b);
    return fragment;
  }

  @BindView(R.id.tv_tip) TextView mTvTip;
  @BindView(R.id.iv_portrait) ImageView mIvPortrait;
  @BindView(R.id.tv_name) TextView mTvName;
  @BindView(R.id.iv_vip) ImageView mIvVip;
  @BindView(R.id.tv_reg_time) TextView mTvRegTime;
  @BindView(R.id.sw_admin) ShSwitchView mSwAdmin;
  @BindView(R.id.btn_add) Button mBtnAdd;

  private FriendshipManagerPresenter friendshipManagerPresenter;

  private MemberVipInfo mVipInfo;

  @Override protected int getLayoutResId() {
    return R.layout.fragment_member_profile;
  }

  @Override protected void initPresenter() {
    presenter = new MemberProfilePresenter(getActivity(), this, this);
    presenter.init();
  }

  @Override public void initView() {

    StatusBarHelper.setStatusBarLightMode(getActivity());
    if (null == getArguments()) {
      popBackStack();
      return;
    } else {
      mVipInfo = (MemberVipInfo) getArguments().getSerializable(INFO_KEY);
    }

    if (FriendshipInfo.getInstance().isFriend(mVipInfo.getId())) {
      mTvTip.setVisibility(View.GONE);
      mBtnAdd.setVisibility(View.GONE);
    } else {
      mTvTip.setVisibility(View.VISIBLE);
      mBtnAdd.setVisibility(View.VISIBLE);
    }
    friendshipManagerPresenter = new FriendshipManagerPresenter(this);

    Glide.with(this)
        .load(mVipInfo.getAvatarUrl())
        .apply(new RequestOptions()
            .bitmapTransform(new GlideCircleBorderTransform(
                mActivity.getResources().getDimension(R.dimen.portrait_border_width),
                ContextCompat.getColor(mActivity, R.color.white)))
            .placeholder(R.drawable.icon_conversation_de)
            .error(R.drawable.icon_conversation_de))
        .into(mIvPortrait);

    mTvName.setText(mVipInfo.getName());

    if (mVipInfo.getLevel() == 1) {
      mIvVip.setImageResource(R.drawable.ic_vip1);
    } else if (mVipInfo.getLevel() == 2) {
      mIvVip.setImageResource(R.drawable.ic_vip2);
    } else if (mVipInfo.getLevel() == 3) {
      mIvVip.setImageResource(R.drawable.ic_vip3);
    } else if (mVipInfo.getLevel() == 4) {
      mIvVip.setImageResource(R.drawable.ic_vip4);
    } else {
      mIvVip.setImageResource(R.drawable.ic_vip5);
    }

    mTvRegTime.setText(mVipInfo.getCreate_date());

    if (mVipInfo.getType() == TIMGroupMemberRoleType.Admin) {
      mSwAdmin.setOn(true);
    } else {
      mSwAdmin.setOn(false);
    }
    mSwAdmin.setOnSwitchStateChangeListener(mChangeListener);
  }

  @OnClick({ R.id.iv_back, R.id.btn_add, R.id.tv_del, R.id.layout_silence }) public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.iv_back:
        popBackStack();
        break;
      case R.id.btn_add:
        addFriend();
        break;
      case R.id.tv_del:
        presenter.deleteMember(mVipInfo.getGroupId(), mVipInfo.getId());
        break;
      case R.id.layout_silence:
        startFragment(SilenceFragment.create(mVipInfo));
        break;
    }
  }

  private void addFriend() {
    EditDialogBuilder.getInstance(mActivity)
        .setTitle("好友验证信息")
        .setHint("请输入验证信息")
        .setCancel("取消", null)
        .setConfirm("确定", content -> {
          showLoadingDialog();
          friendshipManagerPresenter.addFriend(mVipInfo.getId(), "", "", content);
        }).show();
  }

  ShSwitchView.OnSwitchStateChangeListener mChangeListener =
      new ShSwitchView.OnSwitchStateChangeListener() {
        @Override public void onSwitchStateChange(boolean isOn) {
          presenter.setRoleType(mVipInfo.getGroupId(), mVipInfo.getId(), isOn);
        }
      };

  @Override public void setRoleTypeFail() {
    mSwAdmin.setOnSwitchStateChangeListener(null);
    mSwAdmin.setOn(!mSwAdmin.isOn());
    mSwAdmin.setOnSwitchStateChangeListener(mChangeListener);
  }

  @Override public void deleteMemberSuc() {
    popBackStack();
  }

  @Override public void onAddFriend(TIMFriendStatus status) {
    cancelLoadingDialog();
    switch (status) {
      case TIM_ADD_FRIEND_STATUS_PENDING:
        showToast("请求已发送");
        break;
      case TIM_FRIEND_STATUS_SUCC:
        showToast("添加成功");
        break;
      case TIM_ADD_FRIEND_STATUS_FRIEND_SIDE_FORBID_ADD:
        showToast("对方拒绝添加任何人为好友");
        break;
      case TIM_ADD_FRIEND_STATUS_IN_OTHER_SIDE_BLACK_LIST:
        showToast("您在对方的黑名单中");
        break;
      case TIM_ADD_FRIEND_STATUS_IN_SELF_BLACK_LIST:
        showToast("对方在您的黑名单中");
        break;
      default:
        showToast("添加好友失败,请稍后再试");
        break;
    }
  }

  @Override public void onDelFriend(TIMFriendStatus status) {

  }

  @Override public void onChangeGroup(TIMFriendStatus status, String groupName) {

  }

  @Override public void onResume() {
    super.onResume();
    StatusBarHelper.setStatusBarLightMode(getActivity());
  }
}
